/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.inftel.primeproject.managedBeans;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.inftel.primeproject.model.Users;
import org.inftel.primeproject.remote.UsersFacadeRemote;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author inftel
 */
@ManagedBean
@RequestScoped
public class SignUpBean {
    @EJB
    private UsersFacadeRemote userFacade;

    @Size(min = 6)
    private String password;
    @NotNull
    private String name;
    @NotNull
    private String lastname;
 
    @Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{2,3}")    
    private String email;
    
    @NotNull
    private Date birthday;
    
    @NotNull
    private String city;
    
    private char sex;
    private boolean skip;
    private static final Logger logger = Logger.getLogger(SignUpBean.class.getName());
    private Users users;
    private Date now = new Date();

    public Date getNow() {
        return now;
    }

    public void setNow(Date now) {
        this.now = now;
    }
    
    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    /**
     * Creates a new instance of SignUpBean
     */
    public SignUpBean() {
        this.password = null;
        this.name = null;
        this.lastname = null;
        this.email = null;
        this.birthday = null;
        this.city = null;
        this.sex = ' ';
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthay) {
        this.birthday = birthay;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    // Maneja la imagen cargada
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        UploadedFile file = event.getFile();
        Users u = userFacade.findByMail(email);     
        
        String fileName = event.getFile().getFileName();
        String contentType = event.getFile().getContentType();
        byte[] bytes = event.getFile().getContents();
        String photo=new String(bytes);
               
        u.setPhoto(photo);
    }

    public void goBack() {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Register", "DONE");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
        String ctxPath = ((ServletContext) ctx.getContext()).getContextPath();
        try {
            ctx.redirect(ctxPath + "/faces/index.xhtml");
        } catch (IOException ex) {
        }
    }

    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  

        if (skip) {
            skip = false;   //reset in case user goes back  
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }
    
    public void register(){
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg;
        Users user = new Users(name, lastname, email, birthday, sex, city, password);
        userFacade.create(user);
        msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Register:", "success");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("success", true); 
    }
}
